package com.eatrip.presentation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class Pagina extends TagSupport {
	private int page = 1;
	private int numeroElementi = 5;
	private String link = "../../../eatrip/home";
	private Object elemento = null;
	
	private static final long serialVersionUID = -51323829735418201L;
	
	public int doStartTag() throws JspException {
		try{
			JspWriter out = super.pageContext.getOut();
			out.print("<div id=\"paginazione\">\n");
			if (this.elemento != null){
				out = this.setContenuto(out);
			}
			else{
				out = this.setContenutoPrecedente(out);
			}
		} catch(IOException ioe) {
            throw new JspException("Error: " + ioe.getMessage());
        }       
        return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException{
		try{
			JspWriter out = super.pageContext.getOut();
			out.print("</div>");
		}catch(Exception e){
			throw new JspException();
		}
		
		return EVAL_PAGE;
	}
	
	

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getNumeroElementi() {
		return numeroElementi;
	}

	public void setNumeroElementi(int numeroElementi) {
		this.numeroElementi = numeroElementi;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public Object getElemento() {
		return elemento;
	}

	public void setElemento(Object elemento) {
		this.elemento = elemento;
	}
	
	
	public int getNumeroElementiInLista(){
		if (this.elemento instanceof List<?>){
			List<?> tmp = (List<?>) this.elemento;
			return tmp.size();
		}
		else if (this.elemento instanceof Collection<?>){
			Collection<?> tmp = (Collection<?>) this.elemento;
			return tmp.size();
		}
		else if (this.elemento instanceof ArrayList<?>){
			ArrayList<?> tmp = (ArrayList<?>) this.elemento;
			return tmp.size();
		}
		else if (this.elemento instanceof HashMap<?,?>){
			HashMap<?,?> tmp = (HashMap<?,?>) this.elemento;
			return tmp.size();
		}
		return 0;
	}
	
	
	public JspWriter setContenuto(JspWriter out) throws IOException{
		int numElementiInLista = getNumeroElementiInLista();
		//numero di elementi minore di numelementi predefinito
		if (((numElementiInLista >= (this.numeroElementi*2 )) && this.page == 1)){
			//inserirsco due pagine
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ "+ Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+2))," [ " + Integer.toString(this.page+2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva ]");
			
			return out;
		}
		else if (((numElementiInLista >= this.numeroElementi) && this.page == 1)){
			//inserirsco una pagine
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ " + Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva ]");
			
			return out;
		}
		else if (((numElementiInLista >= (this.numeroElementi*2)) && this.page == 2)){
			//inserirsco una pagine
			out = this.setLinkPage(out,(this.link+this.page), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-1)), " [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ " + Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+2))," [ " + Integer.toString(this.page+2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva] ");
		
			return out;
		}
		else if (((numElementiInLista >= (this.numeroElementi)) && this.page == 2)){
			//inserirsco una pagine
			out = this.setLinkPage(out,(this.link+this.page), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ " + Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva ]");
		
			return out;
		}
		else if (((numElementiInLista >= (this.numeroElementi*2)) && this.page > 2)){
			//inserirsco una pagine
			out = this.setLinkPage(out,(this.link+this.page), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-2))," [ " + Integer.toString(this.page-2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ " + Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+2))," [ " + Integer.toString(this.page+2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva ]");
		
			return out;
		}
		else if (((numElementiInLista >= (this.numeroElementi)) && this.page > 2)){
			//inserirsco una pagine
			out = this.setLinkPage(out,(this.link+this.page), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-2))," [ " + Integer.toString(this.page-2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			out = this.setLinkPage(out,(this.link+(this.page+1))," [ " + Integer.toString(this.page+1)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page+1)), " [ Succesiva ]");
		
			return out;
		}
		else if (this.page > 2){
			out = this.setLinkPage(out,(this.link+(this.page-1)), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-2))," [ " + Integer.toString(this.page-2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			
			return out;
		}
		else if (this.page > 1){
			out = this.setLinkPage(out,(this.link+(this.page-1)), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			
			return out;
		}
		else if (this.page == 1){
			return out;
		}
		if (numElementiInLista == 0)
			return out;
		
		return out;
	}
	
	public JspWriter setContenutoPrecedente(JspWriter out) throws IOException{
		if (this.page > 2){
			out = this.setLinkPage(out,(this.link+(this.page-1)), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-2))," [ " + Integer.toString(this.page-2)+ " ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			
			return out;
		}
		else if (this.page > 1){
			out = this.setLinkPage(out,(this.link+(this.page-1)), " [ Precedente ]");
			out = this.setLinkPage(out,(this.link+(this.page-1))," [ " + Integer.toString(this.page-1)+ " ]");
			out = this.setLinkPage(out,(this.link+this.page), " <b>[ Corrente ]</b>");
			
			return out;
		}
		
		return out;
	}
	
	
	
	
	public JspWriter setLinkPage(JspWriter out,String href,String span) throws IOException{
		out.print("<a href=\""+ href +"\"><span class=\"indice-pagina\">"+ span +"</span></a>\n");
		return out;
	}
}
